﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="StrategyForm.cs" company="">
//   
// </copyright>
// <summary>
//   The strategy form.
// </summary>
// --------------------------------------------------------------------------------------------------------------------


using System;
using System.Data.EntityClient;
using System.Data.Objects;
using System.Data.SqlClient;
using System.Drawing;
using System.Windows.Forms;
using Systemathics.FrameWork;
using Systemathics.FrameWork.Interfaces;
using Systemathics.UserControls.Chart;
using Systemathics.UserControls.Chart.Properties;

namespace Systemathics.UserControls
{
    public partial class DataForm : Form
    {
        private IDataProvider backTestdataProvider;

        public DataForm()
        {
            InitializeComponent();
            Icon = Icon.FromHandle(Resources.MSN_Mobile.GetHicon());
            using (var db = new TickCapturesEntities(EntitiesConnectionString()))
                StrategyGrid.DataSource = db.DATES.Execute(MergeOption.AppendOnly);
        }

        private const string TICKCAPTURES_DATABASE_NAME = "TickCaptures";
        private static string SqlConnectionString(string dbName)
        {
            var sqlBuilder = new SqlConnectionStringBuilder
            {
                DataSource = ".\\SQLEXPRESS2008",
                InitialCatalog = dbName,
                IntegratedSecurity = true,
                MultipleActiveResultSets = true
            };
            return sqlBuilder.ToString();
        }
        private static string EntitiesConnectionString()
        {
            var entityBuilder = new EntityConnectionStringBuilder
            {
                Provider = "System.Data.SqlClient",
                ProviderConnectionString = SqlConnectionString(TICKCAPTURES_DATABASE_NAME),
                Metadata = @"res://*/Model.csdl|res://*/Model.ssdl|res://*/Model.msl"
            };

            return entityBuilder.ToString();
        }

        private void QuotestoolStripButton_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewRow row in StrategyGrid.SelectedRows)
            {
                if (row == null)
                    return;
                if (row.Index > -1)
                {
                    var id = new Guid(StrategyGrid["Instrument", row.Index].Value.ToString());
                    var start = (DateTime)StrategyGrid["FirstStartDate", row.Index].Value;
                    var end = (DateTime)StrategyGrid["LastEndDate", row.Index].Value;
                    backTestdataProvider = ProviderFactory.GetDataProvider(StrategyMode.BackTest.ToString(), GlobalVariables.ASSEMBLY);
                    backTestdataProvider.Connect();
                    if (backTestdataProvider.isConnected)
                    {
                        backTestdataProvider.EmitQuoteSeries += EmitQuoteSeriesFromDB;
                        backTestdataProvider.GetHistoricalData(start, end, Data.Quote, id, null);
                    }
                }
            }
        }
        private void EmitQuoteSeriesFromDB(object sender, QuoteSeries e)
        {
            if (e.Count > 0)
            {
                new HistoricalChartForm(e).Show();
                backTestdataProvider.Disconnect();
            }
            else
                new RemotingLogClient().Trace(new FireMessageEventArgs("Count = 0 for " + e.Ticker, DateTime.Now.TimeOfDay));
        }

        private void TradestoolStripButton_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewRow row in StrategyGrid.SelectedRows)
            {
                if (row == null)
                    return;
                if (row.Index > -1)
                {
                    var id = new Guid(StrategyGrid["Instrument", row.Index].Value.ToString());
                    var start = (DateTime)StrategyGrid["FirstStartDate", row.Index].Value;
                    var end = (DateTime)StrategyGrid["LastEndDate", row.Index].Value;
                    backTestdataProvider = ProviderFactory.GetDataProvider(StrategyMode.BackTest.ToString(),
                                                                           GlobalVariables.ASSEMBLY);
                    backTestdataProvider.Connect();
                    if (backTestdataProvider.isConnected)
                    {
                        backTestdataProvider.EmitTradeSeries += EmitTradeSeriesFromDB;
                        backTestdataProvider.GetHistoricalData(start, end, Data.Quote, id, null);
                    }
                }
            }
        }
        private void EmitTradeSeriesFromDB(object sender, TradeSeries e)
        {
            if (e.Count > 0)
            {
                new HistoricalChartForm(e).Show();
                backTestdataProvider.Disconnect();
            }
            else
                new RemotingLogClient().Trace(new FireMessageEventArgs("Count = 0 for " + e.Ticker, DateTime.Now.TimeOfDay));
        }

        private void BarstoolStripButton_Click(object sender, EventArgs e)
        {
            // if dialog box == OK ...take BarSize ...
            var BarSize = 60;
            foreach (DataGridViewRow row in StrategyGrid.SelectedRows)
            {
                if (row == null)
                    return;
                if (row.Index > -1)
                {
                    var id = new Guid(StrategyGrid["Instrument", row.Index].Value.ToString());
                    var start = (DateTime)StrategyGrid["FirstStartDate", row.Index].Value;
                    var end = (DateTime)StrategyGrid["LastEndDate", row.Index].Value;
                    backTestdataProvider = ProviderFactory.GetDataProvider(StrategyMode.BackTest.ToString(),
                                                                           GlobalVariables.ASSEMBLY);
                    backTestdataProvider.Connect();
                    if (backTestdataProvider.isConnected)
                    {
                        backTestdataProvider.EmitBarSeries += EmitBarSeriesFromDB;
                        backTestdataProvider.GetHistoricalData(start, end, Data.Bar, id, BarSize);
                    }
                }
            }
        }
        private void EmitBarSeriesFromDB(object sender, BarSeries e)
        {
            if (e.Count > 0)
            {
                new HistoricalChartForm(e).Show();
                backTestdataProvider.Disconnect();
            }
            else
                new RemotingLogClient().Trace(new FireMessageEventArgs("Count = 0 for " + e.Ticker, DateTime.Now.TimeOfDay));
        }
    }
}